package com.gmlive.common.sample.widgets

import android.content.Intent
import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import android.text.*
import android.text.style.ForegroundColorSpan
import com.gmlive.common.sample.R
import com.gmlive.common.sample.svga.FullScreenSvgaActivity
import com.gmlive.common.sample.svga.SVGAUtils
import com.gmlive.common.ui.app.IkCompatActivity
import com.gmlive.common.ui.app.annotation.Immerse
import com.gmlive.svgaplayer.util.load
import com.gmlive.svgaplayer.util.loadAsset
import com.opensource.svgaplayer.SVGACallback
import kotlinx.android.synthetic.main.activity_svga.*
import java.net.URL

/**
 * Created by ZhongJianXin
 * on 2020/4/14
 * desc
 */
@Immerse(fitSystemWindows = true)
class SVGAActivity : IkCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_svga)

        recycler_button.setOnClickListener {
            startActivity(Intent(this@SVGAActivity, FullScreenSvgaActivity::class.java))
        }

        // 同时加载相同资源的会走两边加载的逻辑，暂时无法避免
        svga_zie_120.loops = 1
        svga_zie_120.callback = object : SVGACallback {
            override fun onPause() {
            }

            override fun onFinished() {
                svga_zie_120.stepToPercentage(1.0, false)
            }

            override fun onRepeat() {
            }

            override fun onStep(frame: Int, percentage: Double) {
            }
        }
        svga_zie_120.load("http://medtx.ikstatic.cn/MTU4NTIxOTI1MjY3OSMyNTgjc3ZnYQ==.svga") {
            listener(onSuccess = { _, _ ->
                svga_zie_120.startAnimation()
            })
        }
        svga_zie_76.load("http://medtx.ikstatic.cn/MTU4NTIxOTI1MjY3OSMyNTgjc3ZnYQ==.svga")

        svga_assert.loadAsset("kingset.svga") {
            setDynamicEntity {
                setStaticLayout("banner", createStaticLayout())

                setDrawer("banner") { canvas, frameIndex ->
                    val aPaint = Paint()
                    aPaint.color = Color.WHITE
                    canvas.drawCircle(50f, 54f, (frameIndex % 5).toFloat(), aPaint)
                    false
                }

                // 设置在线图片
                // setImage("99", "https://media.xiangshengclub.com/MTU5NjIxNTE0NDM3NCM5NzkjanBn.jpg")
            }
        }

        // load via java
        SVGAUtils.loadSvgaFromAssets(svga_java, "colorful_deer.svga")
    }

    private fun createStaticLayout(): StaticLayout {
        val spannableStringBuilder = SpannableStringBuilder("Pony 送了一打风油精给主播")
        spannableStringBuilder.setSpan(ForegroundColorSpan(Color.YELLOW), 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
        val textPaint = TextPaint()
        textPaint.color = Color.WHITE
        textPaint.textSize = 28f

        return StaticLayout(
                spannableStringBuilder,
                0,
                spannableStringBuilder.length,
                textPaint,
                0,
                Layout.Alignment.ALIGN_CENTER,
                1.0f,
                0.0f,
                false
        )
    }
}